﻿Imports System.Xml
Imports System.Math

Public Class Region
	Dim maxx, maxy As Integer
	Dim x1, y1, x2, y2 As Integer
	Public Tag As String

	Sub New(maxx As Integer, maxy As Integer)
		Me.maxx = maxx
		Me.maxy = maxy
	End Sub

	Dim a(3) As Integer

	Sub New(maxx As Integer, maxy As Integer, a As Integer())
		Me.New(maxx, maxy)
		For i = 0 To 3
			Me.a(i) = a(i)
		Next
		Update()
	End Sub

	Event Change()

	Sub New(Node As XmlElement)
		Type = Node.Attributes("Type").Value
		Tag = Node.Attributes("Tag").Value
		Text = Node.Attributes("Area").Value
	End Sub

	ReadOnly Property AllSelected As Boolean
		Get
			Return a(0) = -1 And a(1) = -1
		End Get
	End Property

	Property Type As String

	Sub Save(Doc As XmlDocument, Element As XmlElement)
		Dim CellNode As XmlElement = Element.AppendChild(Doc.CreateElement("Region"))
		CellNode.SetAttribute("Type", Type)
		CellNode.SetAttribute("Tag", Tag)
		CellNode.SetAttribute("Area", Text)
	End Sub

	Property Text As String
		Get
			Return a(0) & ";" & a(1) & ";" & a(2) & ";" & a(3)
		End Get
		Set(value As String)
			Dim c = value.Split(";")
			a(0) = c(0)
			a(1) = c(1)
			a(2) = c(2)
			a(3) = c(3)
			Update()
		End Set
	End Property

	Sub Update()
		RaiseEvent Change()
		x1 = If(a(0) = -1, 1, Min(a(0), a(2)))
		y1 = If(a(1) = -1, 1, Min(a(1), a(3)))
		x2 = If(a(0) = -1, maxx - 1, Max(a(0), a(2)))
		y2 = If(a(1) = -1, maxy - 1, Max(a(1), a(3)))
		RaiseEvent Change()
	End Sub

	Function ContainsY(y As Integer) As Boolean
		Return y >= y1 AndAlso y <= y2
	End Function

	Function ContainsX(x As Integer) As Boolean
		Return x >= x1 AndAlso x <= x2
	End Function

	Function Contains(x As Integer, y As Integer) As Boolean
		Return ContainsX(x) And ContainsY(y)
	End Function

	Function GetArea(Sheet As Sheet) As Rectangle
		Dim R1 = Sheet.DisplayRect(x1, y1)
		Dim R2 = Sheet.DisplayRect(x2, y2)
		Return New Rectangle(R1.Left, R1.Top, R2.Right - R1.Left, R2.Bottom - R1.Top)
	End Function

	Function GetAreaX(Sheet As Sheet) As Rectangle
		Dim R1 = Sheet.DisplayRect(x1, 0)
		Dim R2 = Sheet.DisplayRect(x2, 0)
		Return New Rectangle(R1.Left, R1.Top, R2.Right - R1.Left, R2.Bottom - R1.Top)
	End Function

	Function GetAreaY(Sheet As Sheet) As Rectangle
		Dim R1 = Sheet.DisplayRect(0, y1)
		Dim R2 = Sheet.DisplayRect(0, y2)
		Return New Rectangle(R1.Left, R1.Top, R2.Right - R1.Left, R2.Bottom - R1.Top)
	End Function

	Function Clone() As Region
		Return New Region(maxx, maxy, a)
	End Function

	Sub Move(x As Integer, y As Integer)
		a(0) = x
		a(1) = y
		a(2) = x
		a(3) = y
		Update()
	End Sub

	Sub MoveEnd(x As Integer, y As Integer)
		a(2) = If(a(0) = -1, -1, x)
		a(3) = If(a(1) = -1, -1, y)
		Update()
	End Sub

	ReadOnly Property ColumnSelected As Boolean
		Get
			Return a(0) = -1
		End Get
	End Property

	ReadOnly Property RowSelected As Boolean
		Get
			Return a(1) = -1
		End Get
	End Property

	Sub OffsetEnd(x As Integer, y As Integer)
		a(2) += x
		a(3) += y
		Update()
	End Sub

	Function Contains(rg As Region) As Boolean
		Return x1 <= rg.x1 AndAlso x2 >= rg.x2 AndAlso y1 <= rg.y1 AndAlso y2 >= rg.y2
	End Function

End Class
